package com.wangan.wanganpush.xml;

import com.wangan.wanganpush.model.UserLogin;
import com.wangan.wanganpush.utils.AppConstants;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class UserLoginXml {

    public  String parseXml(List<UserLogin> urList,String messageCode,String fileName) {
        if (urList == null || urList.size() == 0) return null;

        try {
            StringBuffer sb = new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>  <MESSAGE>");
            sb.append("<DATASET name=\"WA_COMMON_010000\">  <DATA>");
            sb.append("<ITEM key=\"H010001\" val=\"" + AppConstants.applicationCoding + "\"/>");
            sb.append("<ITEM key=\"I010014\" val=\"" + messageCode + "\"/>");
            sb.append("<ITEM key=\"I010031\" val=\"" + "LOGIN" + "\"/>");
            sb.append("<ITEM key=\"I010013\" val=\"" + 3 + "\"/>");
            sb.append("</DATA> </DATASET> ");

            sb.append("<DATASET name=\"WA_COMMON_010022\">");

            for (int i = 0; i < urList.size(); i++) {
                UserLogin userLogin = urList.get(i);
                sb.append("<DATA>");
                if (userLogin.getLoginName() != null) {
                    sb.append("<ITEM key=\"B040002\" val=\"" + userLogin.getLoginName() + "\"/>");
                }

                if (userLogin.getUserLoginId() != null) {
                    sb.append("<ITEM key=\"B040003\" val=\"" + userLogin.getUserLoginId() + "\"/>");
                }

                if (userLogin.getUserLoginTime() != null) {
                    sb.append("<ITEM key=\"F040005\" val=\"" + userLogin.getUserLoginTime() + "\"/>");
                }

                if (userLogin.getActionType() != null) {
                    sb.append("<ITEM key=\"H010003\" val=\"" + userLogin.getActionType() + "\"/>");
                }

                if (userLogin.getIpAddress() != null) {
                    sb.append("<ITEM key=\"F020004\" val=\"" + userLogin.getIpAddress() + "\"/>");
                }

                if (userLogin.getPort() != null) {
                    sb.append("<ITEM key=\"F020006\" val=\"" + userLogin.getPort() + "\"/>");
                }

                if (userLogin.getLoginSource() != null) {
                    sb.append("<ITEM key=\"H070003\" val=\"" + userLogin.getLoginSource() + "\"/>");
                }

                if (userLogin.getTerminalMacAddress() != null) {
                    sb.append("<ITEM key=\"F030002\" val=\"" + userLogin.getTerminalMacAddress() + "\"/>");
                }

                if (userLogin.getLoginPhone() != null) {
                    sb.append("<ITEM key=\"B020005\" val=\"" + userLogin.getLoginPhone() + "\"/>");
                }

                if (userLogin.getMobileTerminalImel() != null) {
                    sb.append("<ITEM key=\"C050001\" val=\"" + userLogin.getMobileTerminalImel() + "\"/>");
                }

                if (userLogin.getMobileTerminalImsl() != null) {
                    sb.append("<ITEM key=\"B020007\" val=\"" + userLogin.getMobileTerminalImsl() + "\"/>");
                }

                if (userLogin.getHardwareFeatureString() != null) {
                    sb.append("<ITEM key=\"F030002\" val=\"" + userLogin.getHardwareFeatureString() + "\"/>");
                }

                if (userLogin.getLongitude() != null) {
                    sb.append("<ITEM key=\"F010001\" val=\"" + userLogin.getLongitude() + "\"/>");
                }

                if (userLogin.getLatitude() != null) {
                    sb.append("<ITEM key=\"F010002\" val=\"" + userLogin.getLatitude() + "\"/>");
                }

                if (userLogin.getPositionInformation() != null) {
                    sb.append("<ITEM key=\"I020017\" val=\"" + userLogin.getPositionInformation() + "\"/>");
                }

                if (userLogin.getBaseStationId() != null) {
                    sb.append("<ITEM key=\"F030002\" val=\"" + userLogin.getBaseStationId() + "\"/>");
                }

                if (userLogin.getBaseStationAddress() != null) {
                    sb.append("<ITEM key=\"F030005\" val=\"" + userLogin.getBaseStationAddress() + "\"/>");
                }

                if (userLogin.getTerminalType() != null) {
                    sb.append("<ITEM key=\"C020017\" val=\"" + userLogin.getTerminalType() + "\"/>");
                }

                if (userLogin.getTerminalStyle() != null) {
                    sb.append("<ITEM key=\"C020011\" val=\"" + userLogin.getTerminalStyle() + "\"/>");
                }

                if (userLogin.getTerminalOperationType() != null) {
                    sb.append("<ITEM key=\"C020009\" val=\"" + userLogin.getTerminalOperationType() + "\"/>");
                }

                if (userLogin.getTerminalOperationVersion() != null) {
                    sb.append("<ITEM key=\"C020005\" val=\"" + userLogin.getTerminalOperationVersion() + "\"/>");
                }
                sb.append("</DATA>");
            }

            sb.append("</DATASET>");
            sb.append("</MESSAGE>");


            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}